/*
 * Copyright (C) 2011 Urals State University,
 * Department of Mathematics and Mechanics
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ru.edu.usu.cs.bubbles.engine.box2d;

import ru.edu.usu.cs.bubbles.engine.Fan;
import ru.edu.usu.cs.bubbles.engine.box2d.colliders.Collider;
import ru.edu.usu.cs.bubbles.utils.Point;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;

public class Box2dFan extends Fan implements Box2dEntity {
	private static final Vector2[] defaultFanVertices = {
		new Vector2(0, 0),
		new Vector2(1f, (float) (1/Math.sqrt(3))),
		new Vector2(1f, (float) (-1/Math.sqrt(3)))
	};
	
	private static final Vector2[] defaultFanSensorVertices = {
		new Vector2(4f,(float) (4/Math.sqrt(3))),
		new Vector2(4f, (float) (-4/Math.sqrt(3))),
		
	};
	
	private Body fanBody;
	private float angle;
	private float power;
	
	private Collider collider = null;

	protected Box2dFan(Body fanBody, float power, float angle) {
		this.fanBody = fanBody;
		this.angle = angle;
		this.power = power;
		
		PolygonShape fanShape = new PolygonShape();
		Vector2[] fanVertices = new Vector2[3];
		for(int i = 0; i < 3; ++i)
			fanVertices[i] = defaultFanVertices[i].rotate(angle);
		fanShape.set(fanVertices);
		
		FixtureDef wallFixtureDef = new FixtureDef();
		wallFixtureDef.shape = fanShape;
		Fixture fanFixture = fanBody.createFixture(wallFixtureDef);
		fanFixture.setGameEntity(this);
		fanShape.dispose();

		PolygonShape fanSensorShape = new PolygonShape();
		Vector2[] fanSensorVertices = new Vector2[4];
		for(int i = 0; i < 4; ++i)
			fanSensorVertices[i] = defaultFanSensorVertices[i].rotate(angle);
		fanSensorVertices[2].mul(power);
		fanSensorVertices[3].mul(power);
		fanSensorShape.set(fanSensorVertices);

		FixtureDef fanSensorFixtureDef = new FixtureDef();
		fanSensorFixtureDef.shape = fanSensorShape;
		fanSensorFixtureDef.isSensor = true;
		
		Fixture fanSensorFixture = fanBody.createFixture(fanSensorFixtureDef);
		fanSensorFixture.setGameEntity(this);
		fanSensorShape.dispose();
		
		fanBody.setType(BodyDef.BodyType.StaticBody);
	}
	
	@Override
	public float getAngle() {
		return angle;
	}

	@Override
	public Point getPosition() {
		return fanBody.getPosition();
	}

	@Override
	public Body getBody() {
		return fanBody;
	}

	@Override
	public Collider getCollider() {
		return collider;
	}

	@Override
	public void setCollider(Collider collider) {
		this.collider = collider;
	}
}
